using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Markup;
using System.Windows.Media.Imaging;
using Common;
using Microsoft.Win32;

namespace MainView.Windows
{
	public partial class AddExperimentTemplateWindow : Window, IComponentConnector
	{
		private byte[] _ImageBytes;

		public byte[] ImageBytes
		{
			get
			{
				return _ImageBytes;
			}
			set
			{
				if (value != null)
				{
					_ImageBytes = value;
					if (ImageBytes.Length != 0)
					{
						BitmapImage bitmapImage = new BitmapImage();
						bitmapImage.BeginInit();
						bitmapImage.StreamSource = new MemoryStream(_ImageBytes);
						bitmapImage.EndInit();
						img.Source = bitmapImage;
					}
				}
			}
		}

		public string fileName
		{
			get
			{
				return file_name.Text;
			}
			set
			{
				file_name.Text = value;
			}
		}

		public string remarkName
		{
			get
			{
				return file_remarks.Text;
			}
			set
			{
				file_remarks.Text = value;
			}
		}

		public string simpleName
		{
			get
			{
				return file_simple_name.Text;
			}
			set
			{
				file_simple_name.Text = value;
			}
		}

		public bool? saveIndex
		{
			get
			{
				return checkbox_saveIndex.IsChecked;
			}
			set
			{
				checkbox_saveIndex.IsChecked = value;
			}
		}

		public AddExperimentTemplateWindow()
		{
			InitializeComponent();
		}

		private void btnOK_Click(object sender, RoutedEventArgs e)
		{
			if (!Utility.ValidateFileName(file_name.Text))
			{
				MessageBox.Show(string.Format(Utility.GetMessage("msg_filename_illegalcharacter"), Utility.GetFileNameIllegalCharacter().ToString()));
				file_name.Focus();
			}
			else if (file_name.Text.Equals(string.Empty))
			{
				MessageBox.Show(Utility.GetMessage("msg_filename_is_null"), Utility.GetMessage("Warning"), MessageBoxButton.OK);
			}
			else
			{
				base.DialogResult = true;
				Close();
			}
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			Close();
		}

		private void img_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = Utility.GetMessage("Picture") + "|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;*.png;*.tif;*.tiff";
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			if (openFileDialog.ShowDialog() == true)
			{
				FileStream fileStream = new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
				byte[] array = new byte[fileStream.Length];
				fileStream.Read(array, 0, array.Length);
				fileStream.Close();
				MemoryStream memoryStream = new MemoryStream(array);
				ImageBytes = array;
				Bitmap bitmap = new Bitmap(memoryStream, true);
				IntPtr hbitmap = bitmap.GetHbitmap();
				img.Source = Imaging.CreateBitmapSourceFromHBitmap(hbitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
				memoryStream.Close();
				memoryStream.Dispose();
			}
		}
	}
}
